<template>
  <div class="col main-content">
    <div class="row tool-bar">
      <div class="col">
        <div class="inline-item">
          <el-input v-model="search.name" clearable style="width: 200px;" placeholder="请输入查询信息"/>
          <el-button @click="searchData" icon="el-icon-search" type="primary">查询</el-button>
          <el-button type="primary" @click="createSupply"><i class="fa fa-plus"></i> 添加供应商</el-button>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col">
        <el-table
          :data="supplies"
          border
          tooltip-effect="dark"
          v-loading="loading"
          @selection-change="handleSelectionChange"
          ref="table"
        >
          <el-table-column
            type="selection"
            width="48"
            fixed>
          </el-table-column>
          <el-table-column
            label="ID" width="80"
          >
            <template slot-scope="scope">
              <div class="center">
                <span>{{scope.row.id}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="名称"
          >
            <template slot-scope="scope">{{ scope.row.name }}</template>
          </el-table-column>
          <el-table-column
            label="网址"
          >
            <template slot-scope="scope"><a :href="scope.row.url" target="_blank"> {{ scope.row.url }}</a></template>
          </el-table-column>

          <el-table-column
            label="状态" width="80"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.isShow==1" class="el-alert el-alert--success is-center">
                显示
              </div>
              <div v-else class="el-alert el-alert--error is-center">
                已隐藏
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="90">
            <template slot-scope="scope">
              <div class="center">
                <el-button type="text"
                           @click="doEdit(scope.$index, scope.row)">编辑
                </el-button>

                <el-button type="text"
                           @click="changeStatus(scope.$index, scope.row)">
                  <el-tooltip placement="top" v-if="scope.row.isShow==0">
                    <div slot="content">操作后该供应商将出现在供应商选择列表中</div>
                    <span>显示</span>
                  </el-tooltip>

                  <el-tooltip placement="top" v-else>
                    <div slot="content">操作后该供应商将不会出现在供应商选择列表中</div>
                    <span>隐藏</span>
                  </el-tooltip>
                </el-button>

              </div>
            </template>
          </el-table-column>
        </el-table>

        <div class="col h-pagination">
          <div class="pagination-tool" v-if="selectRows.length>0">
            <el-button type="primary" @click="batchChangeStatus" :loading="batchLoading">变更显示状态</el-button>
          </div>

          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.pagination.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="search.pagination.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="search.pagination.total">
          </el-pagination>
        </div>
      </div>
    </div>
    <admin-supply-edit v-model="currentData" title="供应商编辑" :show.sync="showCreateForm"
                       @created="created"></admin-supply-edit>
  </div>
</template>
<script>
  import AdminService from 'src/service/admin'
  import AdminSupplyEdit from 'src/components/admin/goods/components/SupplyEdit'
  import TableMix from 'src/components/table-mix'

  export default {
    name: 'AdminSupply',
    components: {AdminSupplyEdit},
    mixins: [TableMix],
    data() {
      return {
        supplies: [],
        loading: true,
        search: {
          name: '',
          pagination: {
            total: 0,
            page: 1,
            size: 20
          },
          orderBy: 'is_show desc,id'
        },
        showCreateForm: false,
        currentData: null,
        batchLoading: false,
        changLoading: false,
        selectRows: []
      }
    }
    ,
    methods: {
      doEdit: function (index, data) {
        const me = this;
        this.currentData = data;
        this.$nextTick(function () {
          me.showCreateForm = true
        })
      },
      handleSelectionChange: function (v) {
        this.selectRows = v;
      },
      batchChangeStatus: function () {
        const me = this;
        const ids = this.selectRows.map(ds => {
          return ds.id;
        })
        me.batchLoading = true;

        AdminService.changeSupplyShow({
          ids: ids
        }).then(ds => {
          if (ds != null) {
            me.selectRows.forEach(ds => {
              ds.isShow = ds.isShow == 1 ? 0 : 1;
            })
            me.$message.success("切换显示状态成功。")
          }
          me.$refs.table.clearSelection();
          me.batchLoading = false
        })
      },
      //显示状态变更
      changeStatus: function (index, data) {
        const me = this;
        AdminService.changeSupplyShow({
          ids: [data.id]
        }).then(ds => {
          if (ds != null) {
            data.isShow = data.isShow == 0 ? 1 : 0;
            me.$message.success("切换显示状态成功。")
          }
        })

      },
      handleSizeChange: function (size) {
        this.search.pagination.page = 1;
        this.search.pagination.size = size;
        this.getData();
      },
      handleCurrentChange: function (page) {
        this.search.pagination.page = page;
        this.getData();
      }
      ,
      createSupply: function () {
        const me = this;
        this.currentData = {
          name: '',
          url: '',
          isShow: 1,
          descript: ''
        };
        this.$nextTick(function () {
          me.showCreateForm = true
        })
      }
      ,
      created: function (data) {
        this.showCreateForm = false
        this.search.pagination.page = 1
        this.getData()
      },
      getData: function () {
        const me = this
        me.loading = true
        AdminService.searchSupplies(me.search).then(data => {
          if (data != null) {
            me.supplies = data.datas
            me.search.pagination = data.pagination
          }
          me.loading = false
        })
      },
      searchData: function () {
        this.search.pagination.page = 1
        this.getData()
      }
    },
    beforeMount: function () {
      this.initTalbeHeight(255);
    },
    mounted: function () {
      this.getData();
    }
  }
</script>
